package com.pi.rest.controller;

import java.io.IOException;
import java.util.List;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.pi.file.FileDo;
import com.pi.ibean.IFileDoBean;
import com.pi.springFactory.SpringFactory;



@Controller
@RequestMapping(value="/file")
public class FileController{
	
	 private IFileDoBean iFileBean = null;
	 
	 public FileController(){
		 iFileBean = (IFileDoBean) SpringFactory.getBean("fileDoBean");
	 }
	/**
	 * fileName must contain the path
	 * 约定:路径中的"/"和文件名中的"."用"|"表示 
	 * 比如c:/hxx/xx.txt 表示为c:|hxx|xx|txt
	 * @param fileName
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(method=RequestMethod.GET, value="/{fileName}") 
	public ModelAndView saveFileText(@PathVariable String fileName) { 
	 String str[] = fileName.split("\\|");
	 String filePah = "";
	 for (int i = 0; i < str.length-2; i++)  filePah+=str[i] + "//";
	 filePah += str[str.length-2] +"."+str[str.length-1];
	 List<String> list;
	try {
		list = FileDo.readFile(filePah);
		iFileBean.saveFileText(list);
	} catch (IOException e) {
		e.printStackTrace();
		 return new ModelAndView("fail", "msg", "文件内容保存失败! ");
	}
	 return new ModelAndView("filename", "filename", fileName); 
	}
}

